<template>
  <div>
    <el-table :data="purchases">
      <el-table-column prop="buyNumber" label="采购编号"></el-table-column>
      <el-table-column prop="totalMoney" label="采购总金额(元)"></el-table-column>
      <el-table-column prop="totalNum" label="采购总数量"></el-table-column>
      <el-table-column prop="ptime" label="采购时间"></el-table-column>
      <el-table-column prop="user.uname" label="供应商"></el-table-column>
      <el-table-column label="采购单状态">
        <el-tag type="warning">签收完毕，等待入库</el-tag>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <div v-if="scope.row.isDesign==0">
            <el-button size="mini" type="danger" @click="bePutInStorage(scope.row.id)">设计指定仓库</el-button>
          </div>
          <div v-if="scope.row.isDesign==1">
            <el-button size="mini" type="danger" @click="designPutInStorage(scope.row.id)">修改指定仓库</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageno"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <!--查询等待分配的供应商商品对话框-->
    <div>
      <el-dialog
        title="为商品选择仓库"
        :visible.sync="t1_Visible"
        width="70%">
        <component ref="purchasingSystem_T1dialog" is="purchasingSystem_T1dialog"></component>
        <!--根据采购详情表下的商品维护id，展示-->
        <div>
          <el-dialog
            title="请选择仓库"
            :visible.sync="t2_Visible"
            width="50%"
            append-to-body>
            <component ref="purchasingSystem_T2dialog" is="purchasingSystem_T2dialog"></component>
            <div slot="footer" class="dialog-footer">
              <el-button @click="t2_Visible = false">取 消</el-button>
            </div>
          </el-dialog>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button @click="t1_Visible = false">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
    import purchasingSystem_T1dialog from "./purchasingSystem_T1dialog";
    import purchasingSystem_T2dialog from "./purchasingSystem_T2dialog";
    export default {
        name: "purchasingSystem",
        data() {
          return {
            t1_Visible:false,
            t2_Visible:false,
            purchases: [],
            purchase: {
              buyNumber: "",
              isShipments:2,//到达总店
              isAudit:1//审核通过
              },
            pageno: 1,
            pagesize: 5,
            total: 1
          }
        },
        methods: {
          getPurchases() {
            //每次刷新数据，关闭所有的对话框
            this.t1_Visible=false;
            this.t2_Visible=false;
            var params = new URLSearchParams();
            params.append("pageno", this.pageno);
            params.append("pagesize", this.pagesize);
            Object.keys(this.purchase).forEach(item => {
              params.append(item, this.purchase[item]);
            })
            this.$axios.get("purchase/purchase.action", {params: params})
              .then(res => {
                this.purchases = res.data.records;
                this.total = res.data.total;
              })
              .catch(err => {
                this.$message.error(err)
              })
          },
          bePutInStorage(id){
            //调用异步，根据采购id，添加入库表和入库详情表数据
            this.$axios.get("gatherInfo/addGatherInfoandDetailPurchaseInfo.action/"+id,null)
              .then(res=>{
                if(res.data.x){
                  this.$message.success(res.data.msg);
                  //打开对话框
                  this.t1_Visible=true;
                  //调用子组件函数，进行查询该采购单下的所有详情数据
                  this.$nextTick(item=>{
                    this.$refs.purchasingSystem_T1dialog.getData(id);
                    //重新刷新数据
                    this.getPurchases();
                  });
                }else {
                  this.$message.error(res.data.msg);
                }
              })
              .catch(err=>{
                this.$message.warning(err);
                alert(err);
              })
          },
          designPutInStorage(id){
            //打开对话框
            this.t1_Visible=true;
            //调用子组件函数，进行查询该采购单下的所有详情数据
            this.$nextTick(item=>{
              this.$refs.purchasingSystem_T1dialog.getData(id);
            });
          },
          openPurchasingSystem_T2dialog(id,specifiedState){
            //打开对话框
            this.t2_Visible=true;
            //调用子组件函数
            this.$nextTick(item=>{
              this.$refs.purchasingSystem_T2dialog.getData(id,specifiedState);
            })
          },
          handleSizeChange(val) {
            //每页范围
            this.pagesize = val;
            this.getPurchases();
          },
          handleCurrentChange(val) {
            //当前页
            this.pageno = val;
            this.getPurchases();
          }
        },
        components: {
          purchasingSystem_T1dialog,purchasingSystem_T2dialog
        },
        created() {
          this.getPurchases();
        }
    }
</script>

<style scoped>

</style>
